import React, { Component, PureComponent } from "react";
import { Table, Button, Divider, Modal } from "antd";
import axios from "axios";
// import {connect} from "react-redux"
// import {setVisible,setStudent} from "../actions"
// @connect()
const {confirm} = Modal;
class StudentsList extends PureComponent {
    constructor() {
        super();
    }
    // shouldComponentUpdate = (nextProps, nextState) => {
        // console.log("props",this.props,nextProps);
        // console.log("state",this.state,nextState);
        // return true;
        
    // };

    change = pagination => {
        this.props.show(pagination.current, pagination.pageSize);
    };
    delete = id => {
        console.log(id);
        confirm({
            title: '删除？',
            content: '是否确认要删除？',
            onOk : () => {
                axios({
                    method:"delete",
                    url: "/students/" + id
                }).then(() => {
                    this.props.show();
                });
            }
          });
       
    }
    showById = id => {
        this.props.openUpdate();
        // this.props.dispatch(setVisible(true));
        // this.props.dispatch(setStudent(id));
        // axios({
        //     method: "get",
        //     url: "/students/" + id
        // }).then(({ data }) => {
        //     console.log("data", data);
        //     // this.props.openUpdate(data);
        //     this.props.dispatch(setVisible(true));
            
        // });
    };

    render() {
        console.log("render");
        const columns = [
            {
                title: "姓名",
                dataIndex: "name",
                key: "name"
            },
            {
                title: "年龄",
                dataIndex: "age",
                key: "age"
            },
            {
                title: "性别",
                dataIndex: "gender",
                key: "gender"
            },
            {
                title: "操作",
                key: "action",
                render: (text, record) => (
                    <span>
                        <Button onClick={() => this.showById(record._id)}>
                            修改
                        </Button>
                        <Divider type="vertical" />
                        <Button type="danger" onClick={() => this.delete(record._id)}>删除</Button>
                    </span>
                )
            }
        ];
        return (
            <div>
                <Table
                    Table
                    rowKey={record => record._id}
                    onChange={this.change}
                    dataSource={this.props.students}
                    columns={columns}
                    pagination={this.props.pagination}
                />
            </div>
        );
    }
}
export default StudentsList;
// export default connect()(StudentsList)
